﻿using GameLibrary;
using Microsoft.Xna.Framework;
using Rescue_of_the_Dwarvenzombies.SceneLayers;
using Rescue_of_the_Dwarvenzombies.SceneObjects.GameObjects;

namespace Rescue_of_the_Dwarvenzombies.SceneObjects.GUIObjects
{
    public class TimeBar : GameObject
    {
        public float TimeLeft { get; private set; }

        private Game _game;

        /// <summary>
        /// Initializes a new instance of the Timebar class.
        /// </summary>
        /// <param name="game">The game.</param>
        /// <param name="parentLayer">The parent layer.</param>
        public TimeBar(Game game)
        {
            _game = game;
            TimeLeft = Constant.MAXTIME;
            sourceRectangle = new Rectangle(0, 0, (int)Constant.TIMEBAR_WIDTH, (int)Constant.TIMEBAR_HEIGHT);
            TextureName = Constant.TIMEBAR_TEXTURE_NAME;
            Position = Constant.TIMEBAR_POSITION;
            TintColor = Color.White;
        }

        /// <summary>
        /// Updates the timebar graphicwise and also align it to the camera.
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        public override void Update(GameTime gameTime)
        {
            TimeLeft -= (float)gameTime.ElapsedGameTime.TotalSeconds;
            sourceRectangle.Width = (int)(TimeLeft / Constant.MAXTIME * Constant.TIMEBAR_WIDTH);

            ICamera camera = (ICamera)_game.Services.GetService(typeof(ICamera));
            Position = new Vector2(camera.Position.X + 10, camera.Position.Y + 10);
        }

        /// <summary>
        /// Reduce time equal to damage
        /// </summary>
        /// <param name="damage"></param>
        public override void TakeDamage(float damage)
        {
            TimeLeft -= damage;
        }

        /// <summary>
        /// Add time to timebar, makes sure it never surpass the max time amount
        /// </summary>
        /// <param name="time"></param>
        public void AddTime(float time)
        {
            TimeLeft += time;
            if (TimeLeft >= Constant.MAXTIME)
                TimeLeft = Constant.MAXTIME;
        }
    }
}